import ImagePreviewVue from './rtImagePreview.vue';
// 定义插件对象
const ImagePreview = {};
// vue的install方法，用于定义vue插件
ImagePreview.install = function(Vue, options) {
  const ImagePreviewInstance = Vue.extend(ImagePreviewVue);
  let currentImagePreview;
  const initInstance = () => {
    // 实例化vue实例
    currentImagePreview = new ImagePreviewInstance();
    let ImagePreviewEl = currentImagePreview.$mount().$el;
    document.body.appendChild(ImagePreviewEl);
  };

  // 在Vue的原型上添加实例方法，以全局调用
  Vue.$imagePreview = Vue.prototype.$imagePreview = {
    showImage(params, options) {
      if (!currentImagePreview) {
        initInstance();
      }
      currentImagePreview.showImage(params, options);
    },
    closeImage() {
      currentImagePreview.closeImage();
    },
    isShowPreviewImage() {
      return currentImagePreview
        ? currentImagePreview.isShowPreviewImage()
        : false;
    }
  };
};

export default ImagePreview;
